<template>
  <div class="app-container">

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['wms:goodsCategory:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="Sort"
          @click="toggleExpandAll"
        >展开/折叠</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="goodsCategoryList"
      row-key="id"
      :default-expand-all="isExpandAll"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column label="分类名字" prop="name" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['wms:goodsCategory:edit']">修改</el-button>
          <el-button link type="primary" icon="Plus" @click="handleAdd(scope.row)" v-hasPermi="['wms:goodsCategory:add']">新增</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['wms:goodsCategory:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改商品分类对话框 -->
    <SaveGoodsCategory @submitForm="submitForm" @cancelBox="cancel" :formInfo="form" :goodsCategoryOptions="goodsCategoryOptions" :title="title" :open="open" />

  </div>
</template>

<script setup name="GoodsCategory">
// 导入
import { listGoodsCategory, getGoodsCategory, delGoodsCategory, addGoodsCategory, updateGoodsCategory } from "@/api/wms/goodsCategory";
import SaveGoodsCategory from '@/components/GoodsCategory/save.vue'
components:{
  SaveGoodsCategory
}

const { proxy } = getCurrentInstance();

// 定义变量
const goodsCategoryList = ref([]);
const goodsCategoryOptions = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
//需要传入的字段
const title = ref("");
const isExpandAll = ref(true);
const refreshTable = ref(true);

// 数据定义
const data = reactive({
  form: {},
  queryParams: {
    name: null,
    parentId: null,
    sort: null,
    isDel: null
  }
});

const { queryParams, form } = toRefs(data);

/** 查询商品分类列表 */
function getList() {
  loading.value = true;
  listGoodsCategory(queryParams.value).then(response => {
    goodsCategoryList.value = proxy.handleTree(response.data, "id", "parentId");
    loading.value = false;
  });
}

/** 查询商品分类下拉树结构 */
function getTreeselect() {
  listGoodsCategory().then(response => {
    goodsCategoryOptions.value = [];
    const data = { id: 0, name: '顶级节点', children: [] };
    data.children = proxy.handleTree(response.data, "id", "parentId");
    goodsCategoryOptions.value.push(data);
  });
}
	
// 取消按钮
function cancel() {
  open.value = false;
}

/** 搜索按钮操作 */
function handleQuery() {
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 新增按钮操作 */
function handleAdd(row) {
  reset();
  getTreeselect();
  if (row != null && row.id) {
    form.value.parentId = row.id;
  } else {
    form.value.parentId = 0;
  }
  open.value = true;
  title.value = "添加商品分类";
}

/** 展开/折叠操作 */
function toggleExpandAll() {
  refreshTable.value = false;
  isExpandAll.value = !isExpandAll.value;
  nextTick(() => {
    refreshTable.value = true;
  });
}

/** 修改按钮操作 */
async function handleUpdate(row) {
  await getTreeselect();
  if (row != null) {
    form.value.parentId = row.id;
  }
  getGoodsCategory(row.id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改商品分类";
  });
}


/** 提交按钮 */
function submitForm(formData) {
    if (formData.id != null) {
      updateGoodsCategory(formData).then(response => {
        proxy.$modal.msgSuccess("修改成功");
        open.value = false;
        getList();
      });
    } else {
      addGoodsCategory(formData).then(response => {
        proxy.$modal.msgSuccess("新增成功");
        open.value = false;
        getList();
      });
    }
}

/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$modal.confirm('是否确认删除商品分类编号为"' + row.id + '"的数据项？').then(function() {
    return delGoodsCategory(row.id);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

// 执行getList()方法
getList();
</script>
